//
//  foundQuestViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/31.
//

#import "foundQuestViewController.h"
#import "foundQuestTopheaderView.h"
#import "foundQuestModel.h"
#import "DTChoosePersonListShowView.h"
#import "YTSelectStringShowView.h"
#import "foundQuestSelectPondShowView.h"
#import "foundQuestButtomFooterView.h"
#import "foundQuestShowView.h"
#import "foundQuestHeaderView.h"
#import "YTTipsShowView.h"
#import "foundQuestDXTableViewCell.h"
#import "foundQuestFooterView.h"

#import "TemplateFoundDetailsDXXZTableViewCell.h"
#import "TemplateFoundDetailsDXXLTableViewCell.h"
#import "TemplateFoundDetailsSZSRTableViewCell.h"
#import "TemplateFoundDetailsDHWBTableViewCell.h"
#import "TemplateFoundDetailsDHTableViewCell.h"
#import "foundQuestBGTableViewCell.h"
#import "PersonInCchargeShowView.h"

@interface foundQuestViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) foundQuestModel * model;
@property (nonatomic ,strong) UIView * addRWview;
@end

@implementation foundQuestViewController

- (foundQuestModel *)model{
    if(!_model){
        _model = [foundQuestModel new];
    }
    return _model;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.NoDataHidden = YES;
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"新增任务" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    
    
    UIView * bottomview = [UIView new];
    bottomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bottomview];
    [bottomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    
    UIButton * xybbtn = [UIButton buttonWithType:0];
    [DTKTool YJ:kwt(10) andView:xybbtn];
    xybbtn.addNormalText(@"提交").addNormalColor(UIColor.whiteColor).addTarget(self,@selector(TJApi));
    xybbtn.addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(350), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]);
    [bottomview addSubview:xybbtn];
    [xybbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(8));
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.height.offset(kwt(48));
    }];
    
    UIView * addRWview = [UIView new];
    [DTKTool BFYJ:kwt(kwt(10)) andView:addRWview andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize:CGRectMake(0, 0, KSCREEN_WIDTH, kwt(47))];
    addRWview.addClickBlock(^{
        [weakSelf addRWLXWithModel:weakSelf.model];
    });
    addRWview.clipsToBounds = YES;
    addRWview.backgroundColor = KcolorHex(@"#F6F7F9");
    self.addRWview = addRWview;
    [self.view addSubview:addRWview];
    [addRWview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(bottomview.mas_top);
        make.height.offset(0);
    }];
    
    UILabel * temp1 = [DTKTool labelWithText:@"+选择任务名称" andFont:kCFont(17) andTextColor:KcolorHex(@"#0967FF")];
    [addRWview addSubview:temp1];
    [temp1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#EBEDF0");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(addRWview.mas_top);
    }];
    
    [self tableviewReload];
    
    if(self.pondid){///有池塘id写入数据
        [self openPondIDApi];
    }
}
- (void)openPondIDApi{
    
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondid forKey:@"id"];
    [SCNetManger PostAlarmManageAlarmAddTaskDetailModelWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            self.model = [foundQuestModel yy_modelWithDictionary:model.result];
            self.model.TJtissueName = self.model.tissueName;//额外赋值
//            池塘重新处理
            NSString * pondId = [model.result objectForKey:@"pondId"];
            NSString * pondName = [model.result objectForKey:@"pondName"];
            NSNumber * batchFlag = [model.result objectForKey:@"batchFlag"];
            NSArray * pondIdArray = [pondId componentsSeparatedByString:@","];
            NSArray * pondNameArray = [pondName componentsSeparatedByString:@","];

            for (int i = 0; i < pondIdArray.count ; i ++) {///处理不严谨
                pondModelListModel * pondmodel = [pondModelListModel new];
                pondmodel.ID = pondIdArray[i];
                pondmodel.batchFlag = [NSString stringWithFormat:@"%@",batchFlag];
                if(pondNameArray.count > i){
                    pondmodel.name = pondNameArray[i];
                }
                [self.model.pondModelListModel addObject:pondmodel];
            }
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)TJApi{
    if([self.model TjAPiHUDString].length > 0){
        [self.view showWarning:[self.model TjAPiHUDString]];
        return;
    }
    
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [self.model TjDict];
    if(self.alarmId.length > 0){
        [dict setObject:self.alarmId forKey:@"alarmId"];
    }
    [SCNetManger PostTaskAddWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}


- (void)tableviewReload{
    [self.addRWview mas_updateConstraints:^(MASConstraintMaker *make) {
        if(self.model.taskTypeModelList.count > 0){
            make.height.offset(kwt(47));
        } else {
            make.height.offset(0);
        }
    }];
    
    [self.tableview reloadData];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MJWeakSelf
    if(section == 0){///顶部
        foundQuestTopheaderView * headerview = [foundQuestTopheaderView NewView];
        
        headerview.openTKBZBlock = ^(foundQuestModel * _Nonnull model) {
            [weakSelf TKBZWithModel:model];
        };
        headerview.openTKMBBlock = ^(foundQuestModel * _Nonnull model) {
            [weakSelf GZMBWithModel:model];
        };
        headerview.openTKMCBlock = ^(foundQuestModel * _Nonnull model) {
            [weakSelf TKMCWithModel:model];
        };
        headerview.openRWBlock = ^(foundQuestModel * _Nonnull model) {
            [weakSelf addRWLXWithModel:model];
        };
        headerview.model = self.model;
        return headerview;
    } else {
        foundQuestHeaderView * headerview = [foundQuestHeaderView NewView];
        headerview.model = self.model.taskTypeModelList[section - 1];
        headerview.openPZBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull model) {
            [weakSelf RWPZWithModel:model];
        };
        headerview.openReloadBlock = ^{
            [weakSelf tableviewReload];
        };
        headerview.openRemoveBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull Model) {
            [YTTipsShowView showviewWithTips:@"确定删除该任务吗?" andRightBlock:^{
                [weakSelf.model.taskTypeModelList removeObject:Model];
                [weakSelf tableviewReload];
            }];
        };
        return headerview;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(section == 0){///顶部
        return self.model.HeaderTopViewH;
    } else {
        return self.model.taskTypeModelList[section - 1].RWhaederViewH;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 0){///顶部
        foundQuestButtomFooterView * footview = [foundQuestButtomFooterView NewView];
        return footview;
    } else {
        MJWeakSelf
        foundQuestFooterView * footview = [foundQuestFooterView NewView];
        footview.model = self.model.taskTypeModelList[section - 1];
        footview.openTimeBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull model) {
            [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMDHM title:@"请选择任务执行时间" selectValue:model.executeTime minDate:[NSDate new] maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
                model.executeTime = selectValue;
                [weakSelf.tableview reloadData];
            }];
        };
        footview.openUserBlock = ^(TemplateFoundDetailsTaskTypeModelListModel * _Nonnull model) {
            [weakSelf openRWZPRWithModel:model];
        };
        footview.reLoadBlock = ^{
            [weakSelf.tableview reloadData];
        };
        return footview;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if(section == 0){///顶部
        return kwt(10);
    }else {
        return self.model.taskTypeModelList[section - 1].RWfooterViewH;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.taskTypeModelList.count + 1;
    
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){///顶部
        return 0;
    }
    if(self.model.taskTypeModelList[section - 1].isSQ){
        return 0;
    }
    if(self.model.taskTypeModelList[section - 1].configureItem == 1){///同样配置
        return self.model.taskTypeModelList[section - 1].taskItemModelList.count ;
    }
    return self.model.taskTypeModelList[section - 1].pondModelListModel.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(self.model.taskTypeModelList[indexPath.section - 1].configureItem == 1){///同样配置
        return self.model.taskTypeModelList[indexPath.section - 1].taskItemModelList[indexPath.row].CellH;
    } else {///分别配置
        if(self.model.taskTypeModelList[indexPath.section - 1].isBG){///按照表格显示
            return self.model.taskTypeModelList[indexPath.section - 1].pondModelListModel[indexPath.row].RWBGcellH;
        } else {
            return self.model.taskTypeModelList[indexPath.section - 1].pondModelListModel[indexPath.row].RWcellH - 0.5;
        }
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    if(self.model.taskTypeModelList[indexPath.section - 1].configureItem == 1){///统一配置
        MJWeakSelf
        TemplateFoundDetailsTaskItemModelListModel * Listmodel = self.model.taskTypeModelList[indexPath.section - 1].taskItemModelList[indexPath.row];
        if(Listmodel.pattern == 1) {///多选
            TemplateFoundDetailsDXXZTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDXXZTableViewCell"];
            if (cell == nil) {
                cell = [[TemplateFoundDetailsDXXZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDXXZTableViewCell"];
            }
            cell.model = Listmodel;
            cell.openReloadBlock = ^{
                [weakSelf.tableview reloadData];
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell ;
            
        } else if( Listmodel.pattern == 2){///单选
            TemplateFoundDetailsDXXLTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDXXLTableViewCell"];
            if (cell == nil) {
                cell = [[TemplateFoundDetailsDXXLTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDXXLTableViewCell"];
            }
            cell.model = Listmodel;
            ///下拉单选
            cell.openXLDXBlock = ^(TemplateFoundDetailsTaskItemModelListModel * _Nonnull Model, TemplateFoundDetailsItemModelListModel * _Nonnull itemModel) {
                NSMutableArray * nameArray = [NSMutableArray array];
                int i = 0;
                int selectIndex = 0;
                for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                    [nameArray addObject:itemmodel.name];
                    if(itemmodel == itemModel){
                        selectIndex = i;
                    }
                    i ++;
                }
                [BRStringPickerView showPickerWithTitle:[NSString stringWithFormat:@"请选择%@",Listmodel.name] dataSourceArr:nameArray selectIndex:selectIndex resultBlock:^(BRResultModel * _Nullable resultModel) {
                    for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                        if([itemmodel.name isEqualToString:resultModel.value]){
                            itemmodel.checkedFlag = 1;
                        } else {
                            itemmodel.checkedFlag = 0;
                        }
                    }
                    [weakSelf.tableview reloadData];
                }];
            };    cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell ;
        } else if(Listmodel.pattern == 3){///数字输入
            
            TemplateFoundDetailsSZSRTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsSZSRTableViewCell"];
            if (cell == nil) {
                cell = [[TemplateFoundDetailsSZSRTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsSZSRTableViewCell"];
            }
            cell.model = Listmodel;
            cell.openReloadBlock = ^{
                [weakSelf.tableview reloadData];
            };
            cell.openHUDBlock = ^(NSString * _Nonnull str) {
                [weakSelf.view showWarning:str];
                [weakSelf.tableview reloadData];
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell ;
        } else if(Listmodel.pattern == 4){///单行文本
            TemplateFoundDetailsDHWBTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDHWBTableViewCell"];
            if (cell == nil) {
                cell = [[TemplateFoundDetailsDHWBTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDHWBTableViewCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = Listmodel;
            return cell ;
        } else if(Listmodel.pattern == 5){///多行文本
            
            TemplateFoundDetailsDHTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TemplateFoundDetailsDHTableViewCell"];
            if (cell == nil) {
                cell = [[TemplateFoundDetailsDHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TemplateFoundDetailsDHTableViewCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = Listmodel;
            return cell ;
        }
        
        UITableViewCell * cell =  [UITableViewCell new];
        return cell ;
    } else {///分别配置
       
        if(self.model.taskTypeModelList[indexPath.section - 1].isBG){///按照表格显示
            
            foundQuestBGTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"foundQuestBGTableViewCell"];
            if (cell == nil) {
                cell = [[foundQuestBGTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"foundQuestBGTableViewCell"];
            }
            cell.model = self.model.taskTypeModelList[indexPath.section - 1].pondModelListModel[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.ReloadBlock = ^{
                [weakSelf.tableview reloadData];
            };
            cell.openHUDBlock = ^(NSString * _Nonnull str) {
                [weakSelf.view showWarning:str];
            };
            cell.openXLDXBlock = ^(TemplateFoundDetailsTaskItemModelListModel * _Nonnull Model, TemplateFoundDetailsItemModelListModel * _Nonnull itemModel, NSString * _Nonnull name) {
                
                NSMutableArray * nameArray = [NSMutableArray array];
                int i = 0;
                int selectIndex = 0;
                for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                    [nameArray addObject:itemmodel.name];
                    if(itemmodel == itemModel){
                        selectIndex = i;
                    }
                    i ++;
                }
                [BRStringPickerView showPickerWithTitle:[NSString stringWithFormat:@"请选择%@",name] dataSourceArr:nameArray selectIndex:selectIndex resultBlock:^(BRResultModel * _Nullable resultModel) {
                    for (TemplateFoundDetailsItemModelListModel * itemmodel in Model.itemModelList) {
                        if([itemmodel.name isEqualToString:resultModel.value]){
                            itemmodel.checkedFlag = 1;
                        } else {
                            itemmodel.checkedFlag = 0;
                        }
                    }
                    [weakSelf.tableview reloadData];
                }];
            };
            return cell ;
        } else{
            foundQuestDXTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"foundQuestDXTableViewCell"];
            if (cell == nil) {
                cell = [[foundQuestDXTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"foundQuestDXTableViewCell"];
            }
            cell.model = self.model.taskTypeModelList[indexPath.section - 1].pondModelListModel[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.ReloadBlock = ^{
                [weakSelf.tableview reloadData];
            };
            cell.openHUDBlock = ^(NSString * _Nonnull str) {
                [weakSelf.view showWarning:str];
            };
            return cell ;
        }
    }
}

///池塘模组
-(void)TKBZWithModel:(foundQuestModel *)Model{
    [self.view showBusyHUD];
    [SCNetManger PostPondTissueGetPondTissueListWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <getPondTissueListModel *>* array = [NSArray yy_modelArrayWithClass:getPondTissueListModel.class json:model.result ];
            getPondTissueListModel * mm = [getPondTissueListModel new];
            mm.children = array;
            if(self.model.tissueId.length > 0){
                [mm modelWithSeledID:self.model.tissueId];
            }
            NSLog(@"%@",array);
            MJWeakSelf
            [DTChoosePersonListShowView showViewWithArray:array andBlock:^(getPondTissueListModel * _Nonnull pondModel) {
                weakSelf.model.tissueId = pondModel.ID;
//                weakSelf.model.tissueName = pondModel.name;显示前缀
                weakSelf.model.tissueName = pondModel.TJname;
                weakSelf.model.TJtissueName = pondModel.TJname;
                [weakSelf.model.taskTypeModelList removeAllObjects];
                [weakSelf.model.pondModelListModel removeAllObjects];
                [weakSelf.tableview reloadData];
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

///工作模版
-(void)GZMBWithModel:(foundQuestModel *)Model{
    [self.view showBusyHUD];
    [SCNetManger PostTemplateOptionListWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <templateOptionListModel *>* modelarray = [NSArray yy_modelArrayWithClass:templateOptionListModel.class json:model.result ];
            MJWeakSelf
            int i = 0;
            int index = 0;
            NSMutableArray * array = [NSMutableArray array];
            for (templateOptionListModel * typeModel in modelarray) {
                if([typeModel.ID isEqualToString:Model.templateId]){
                    index = i;
                }
                [array addObject:typeModel.name];
                i ++;
            }
            
            [YTSelectStringShowView showViewWithTitle:@"选择工作模版" andIndex:index andDateArray:array andBlock:^(NSString * _Nonnull str) {
                for (templateOptionListModel * typeModel in modelarray) {
                    if([str isEqualToString:typeModel.name]){
                        weakSelf.model.templateId = typeModel.ID;
                        weakSelf.model.templateName = typeModel.name;
                        [weakSelf.model.taskTypeModelList removeAllObjects];
                        [weakSelf.model.pondModelListModel removeAllObjects];
                        [weakSelf tableviewReload];
                        return;
                    }
                }
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
///池塘名称
-(void)TKMCWithModel:(foundQuestModel *)Model{
    if(Model.tissueId.length < 1){
        [self.view showWarning:@"请先选择池塘编组"];
        return;
    }
    if(Model.templateId.length < 1){
        [self.view showWarning:@"请先选择工作模版"];
        return;
    }
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:Model.templateId forKey:@"templateId"];
    [dict setObject:Model.tissueId forKey:@"tissueId"];
    MJWeakSelf
    [SCNetManger PostTaskSelectPondWithDict:dict  andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <pondModelListModel *>* Modelarray = [NSArray yy_modelArrayWithClass:pondModelListModel.class json:model.result ];
            ///数据清理
            for (pondModelListModel * tempmodel in Modelarray) {
                for (pondModelListModel * oldModel in self.model.pondModelListModel) {
                    if([oldModel.ID isEqualToString:tempmodel.ID]){
                        tempmodel.isSeled = YES;
                    }
                }
                
            }
            
            [foundQuestSelectPondShowView showviewWithArray:Modelarray andBlock:^(NSArray<NSString *> * array) {
                [weakSelf TKSXxWitharray:Modelarray andSeledArray:array];
            }];
            
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}
///池塘名称筛选
- (void)TKSXxWitharray:(NSArray <pondModelListModel *>*) Modelarray andSeledArray:(NSArray <NSString *>*)seledarray{
    
    NSMutableArray <pondModelListModel *>* newArray = [NSMutableArray array];///选中的数据
    NSMutableArray <pondModelListModel *>* pondModelList = [NSMutableArray array];///写入数据的数组
    ///池塘筛选
    for (pondModelListModel * tempmodel in Modelarray) {
        BOOL isHave = NO;
        for (NSString * ID in seledarray) {
            if([ID isEqualToString:tempmodel.ID]){
                isHave = YES;
            }
        }
        if(isHave){
            [newArray addObject:tempmodel];
        }
    }
    for (pondModelListModel * modelNew in newArray) {
        [modelNew.taskItemModelList removeAllObjects];
        BOOL isHave = NO;
        for (pondModelListModel * modelOld in self.model.pondModelListModel) {
            if([modelNew.ID isEqualToString:modelOld.ID]){
                isHave = YES;
                [pondModelList addObject:modelOld];
            }
        }
        if(isHave == NO){
            [pondModelList addObject:modelNew];
        }
    }
    
    self.model.pondModelListModel = pondModelList;
    ///给任务添加池塘
    for (TemplateFoundDetailsTaskTypeModelListModel * takemodel in self.model.taskTypeModelList) {//每个任务
        takemodel.configureItem = 1;///改成默认统一
        NSMutableArray * NewpondModelListModel = [NSMutableArray array];
        for (pondModelListModel * NewPondmodel in pondModelList) {///便利池塘
            BOOL isHave = NO;
            for (pondModelListModel * pondmodel in takemodel.CopyPondModelListModel) {///每个池塘
                if([pondmodel.ID isEqualToString:NewPondmodel.ID]){
                    isHave = YES;
                    [NewpondModelListModel addObject:pondmodel];
                }
            }
            if(isHave == NO){
                
                [NewpondModelListModel addObject:NewPondmodel];
            }
        }
        
        takemodel.pondModelListModel = NewpondModelListModel;
        ///给鱼塘添加任务块
        [takemodel AllPondAddTask];
    }
    [self tableviewReload];
}
///打开任务类型弹窗
- (void)addRWLXWithModel:(foundQuestModel *)model{
    if(model.tissueId.length < 1){
        [self.view showWarning:@"请选择池塘编组"];
        return;
    }
    if(model.templateId.length < 1){
        [self.view showWarning:@"请选择工作模版"];
        return;
    }
    if(model.pondModelListModel.count < 1){
        [self.view showWarning:@"请选择池塘名称"];
        return;
    }
    MJWeakSelf
    [foundQuestShowView showviewWithId:model.templateId andModelArray:@[] andRightBlock:^(NSArray<NSString *> * _Nonnull IDArray) {
        [weakSelf addRWLSWithWithArray:IDArray];
    }];
}
///获取具体任务列表
- (void)addRWLSWithWithArray:(NSArray<NSString *> *) IDArray{
//    NSMutableArray * taskTypeModelList = [NSMutableArray array];
//    for (TemplateFoundDetailsTaskTypeModelListModel * oldModel in self.model.taskTypeModelList) {///先删除的任务
//        oldModel.configureItem = 1;
//        for (NSString * ID in IDArray) {
//            if([oldModel.ID isEqualToString:ID]){
//                [taskTypeModelList addObject:oldModel];
//            }
//        }
//    }
//    self.model.taskTypeModelList = taskTypeModelList;
    
    NSMutableArray * array = [NSMutableArray array];
    for (NSString * ID in IDArray) {
//        BOOL isHave = NO;
//        for (TemplateFoundDetailsTaskTypeModelListModel * oldModel in self.model.taskTypeModelList) {
//            if([oldModel.ID isEqualToString:ID]){
//                isHave = YES;
//            }
//        }
//        if(isHave == NO){
            [array addObject:@{@"id":ID}];
//        }
    }
    [self.view showBusyHUD];
    [SCNetManger PostMBselectTypeWithDict:array andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * taskTypeModelList = [NSArray yy_modelArrayWithClass:TemplateFoundDetailsTaskTypeModelListModel.class json:model.result];
            for (TemplateFoundDetailsTaskTypeModelListModel * typemodel in taskTypeModelList) {
                typemodel.templateTasktypeId = typemodel.ID;
                for (TemplateFoundDetailsTaskItemModelListModel * itemmodel in typemodel.taskItemModelList) {
                    itemmodel.templateTaskitemId = itemmodel.ID;
                    for (TemplateFoundDetailsItemModelListModel * itemlistmodel in itemmodel.itemModelList) {
                        itemlistmodel.templateItemId = itemlistmodel.ID;
                        itemlistmodel.ID = @"";
//                        for (TemplateFoundDetailsItemListModel * list in itemlistmodel.itemList) {
//                            list.templateItemId = list.ID;
//                            for (TemplateFoundDetailsChildrenModelListModel * children in   list.childrenModelList) {
//                                children.templateItemId = list.ID;
//                            }
//                        }
                    }
                }
                if(self.usermodel){
                    [typemodel.appointorArray addObject:self.usermodel];
                }
            }
            
            for (TemplateFoundDetailsTaskTypeModelListModel * tempmodel in taskTypeModelList) {
                NSMutableArray * tempArray = [NSMutableArray arrayWithArray:self.model.CopyPondModelListModel];
                ///任务加入鱼塘
                tempmodel.pondModelListModel = tempArray;
                ///鱼塘添加任务块
                [tempmodel AllPondAddTask];
            }
            ///这里要尝试写入鱼塘数据
            [self.model.taskTypeModelList addObjectsFromArray:taskTypeModelList];
            
            [self tableviewReload];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
///选择任务配置
- (void)RWPZWithModel:(TemplateFoundDetailsTaskTypeModelListModel *)Model{
    MJWeakSelf
    [YTSelectStringShowView showViewWithTitle:@"选择配置" andIndex:Model.configureItem - 1 andDateArray:@[@"统一配置",@"分别配置"] andBlock:^(NSString * _Nonnull str) {
        if([str isEqualToString:@"统一配置"]){
            if(Model.configureItem == 1){
                return;
            }
            Model.configureItem = 1;
        } else if([str isEqualToString:@"分别配置"]){
            if(Model.configureItem == 2){
                return;
            }
            /// 走接口拿参数  分别配置时获取最新参数
            [self TaskConfigureWithModel:Model];
        }
        [weakSelf tableviewReload];
    }];
}

///获取分别配置数据
- (void)TaskConfigureWithModel:(TemplateFoundDetailsTaskTypeModelListModel *)Model{
    [self.view showBusyHUD];
    NSMutableDictionary * dict =  [NSMutableDictionary dictionary];
    [dict setObject:Model.templateTasktypeId forKey:@"templateTasktypeId"];
    NSMutableArray * pond = [NSMutableArray array];
    for (pondModelListModel * pondModel in Model.pondModelListModel) {
        [pond addObject:@{@"id":pondModel.ID}];
    }
    [dict setObject:pond forKey:@"pond"];

    MJWeakSelf
    [SCNetManger PostTaskConfigureWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray<pondModelListModel *> * modelarray = [NSArray yy_modelArrayWithClass:pondModelListModel.class json:model.result];
            Model.pondModelListModel = modelarray.mutableCopy;
            Model.configureItem = 2;
            [weakSelf tableviewReload];

        } else {
            [self.view showWarning:model.message];
        }
    }];
}



//- (void)openTaskConfigure1WithModel:(TemplateFoundDetailsTaskTypeModelListModel *)Model{
////    Model.configureItem = 2;

//    
//}
///
- (void)openRWZPRWithModel:(TemplateFoundDetailsTaskTypeModelListModel *)Model{
    
    [self.view showBusyHUD];
    MJWeakSelf
    [SCNetManger PostGetPositionAndUserByOrgCodeWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <PositionUserModel *>* modelarray = [NSArray yy_modelArrayWithClass:PositionUserModel.class json:model.result];
            
            for (PositionUserModel * pmodel in modelarray) {
                for (PondUserModel * usermodel in pmodel.userModelList) {
                    for (PondUserModel * oldUser in Model.appointorArray) {
                        if ([oldUser.userId isEqualToString:usermodel.userId]) {
                            usermodel.isSeled = YES;
                        }
                    }
                }
            }
            
            [PersonInCchargeShowView showviewWithArray:modelarray andBlock:^(NSArray<PondUserModel *> * array) {
                Model.appointorArray = [NSMutableArray arrayWithArray: array];
                [weakSelf.tableview reloadData];
            }];

      
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
    
}
@end

